import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { FormattedMessage } from 'react-intl'
import { InputItem, Modal, NoticeBar ,Icon, Toast} from 'antd-mobile'
import { hashHistory } from 'react-router'
import { config, myFetch, helper } from '../utils'
import { OrderListItem } from './'




class MarketList extends Component {
  constructor(props) {
    super(props)
    this.state = {
      dataList: []
    }
  }
  onOrderireki = () => {
    const options = {
      method: 'POST',
      body: JSON.stringify({
        PGNO: 1,
        SKKB: 1
      })
    }
    myFetch(`${config.rootApi}/mPositionlist`, options, true)
    //myFetch(`${config.rootApi}/mPosition`, options, true)
    .then((rs) => {
      console.log(rs)
      if (rs && rs.statusCode == 0) {
        let data = rs.dataInfoPosition.dataListPositions
        this.setState({
          dataList: data
        })
      }
    })
  }
  onClose = (data) => {
      let direction
      if (data.bsCls == 0) {
        direction = 1
      } else if (data.bsCls == 1) {
        direction = 0
      }
      const options = {
        method: 'POST',
        body: JSON.stringify({
          PAIR: data.ccypairCd,
          BSKB: direction,
          AMNT: data.positionAmt,
          SKJK: 1,
          KORD: data.orderNo,
          OFLG: 1
        })
      }
      myFetch(`${config.rootApi}/mClosemarket`, options, true)
       .then((rs) => {
          if ( rs && rs.statusCode == 0) {
            Toast.success('平仓成功', 1.2, () => {
              this.onOrderireki()
            })
          }
       })
    
  }
  componentDidMount() {
    this.onOrderireki()
  }
  render() {
    const { dataList } = this.state
    return (
      <div className="-backgrounds">
        <ul className="rt-position-list">
          {
            dataList.map((val, index) => {
              return <OrderListItem
                data={val} 
                key={index} 
                onClose={this.onClose}
                /> 
            })
          }
        </ul>
      </div>
    )
  }
}

export default MarketList

